package ase.eleitweg.server.db;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.apache.log4j.*;

public class DatabaseFactory {

	private static Logger log = Logger.getLogger(DatabaseFactory.class);
	
	private static ELeitwegDAO filledInstance = null;
	
	public static void resetSingleton() {
		log.info("reseting singleton");
		ClassPathResource res = new ClassPathResource("beans.xml");
		XmlBeanFactory xbf = new XmlBeanFactory(res);
		xbf.destroySingleton("eleitwegDAOFilled");
		if(filledInstance!=null) {
			xbf.destroyBean("eleitwegDAOFilled", filledInstance);
			filledInstance.destroy();
		}
	}

	public static ELeitwegDAO getDB() {

		filledInstance = getDB("eleitwegDAOFilled");
		return filledInstance;
	}
	
	public static ELeitwegDAO getDB(String beanid) {

		log.info("Initalizing DAO");
		ClassPathResource res = new ClassPathResource("beans.xml");
		XmlBeanFactory xbf = new XmlBeanFactory(res);
		return (ELeitwegDAO) xbf.getBean(beanid);

	}



}